<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="140px">
<!--          <el-form-item label="nftId">-->
<!--            <el-input v-model="form.nftId" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="productSnapshotId">-->
<!--            <el-input v-model="form.productSnapshotId" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="活动">-->
<!--            <el-input v-model="form.brandId" style="width: 370px;" />-->
<!--          </el-form-item>-->
          <el-form-item label="分类id">
            <el-input v-model="form.categoryId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="nft编码">
            <el-input v-model="form.nftCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="nft名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="总数量">
            <el-input v-model="form.number" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="nft分类名称">
            <el-input v-model="form.nftCategoryName" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="主图">
              <MaterialList
                      v-model="picArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="画册图片">
              <MaterialList
                      v-model="albumPicsArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="上架状态">
            <el-radio v-for="item in dict.nft_status" :key="item.id" v-model="form.publishStatus" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="form.sort" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="金额">
            <el-input v-model="form.price" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="商品重量，默认为克">
            <el-input v-model="form.weight" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产品详情网页内容">
            <el-input :rows="3" v-model="form.detailHtml" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="移动端网页详情">
            <el-input :rows="3" v-model="form.detailMobileHtml" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="品牌名称">
            <el-input v-model="form.brandName" style="width: 370px;" />
          </el-form-item>


<!--          <el-form-item label="创建人">-->
<!--            <el-input v-model="form.createBy" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="创建时间">-->
<!--            <el-input v-model="form.createTime" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="修改人">-->
<!--            <el-input v-model="form.updateBy" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="修改时间">-->
<!--            <el-input v-model="form.updateTime" style="width: 370px;" />-->
<!--          </el-form-item>-->
          <el-form-item label="nft属性，json格式">
            <el-input :rows="3" v-model="form.productAttr" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
<!--        <el-table-column v-if="columns.visible('nftId')" prop="nftId" label="nftId" />-->
<!--        <el-table-column v-if="columns.visible('productSnapshotId')" prop="productSnapshotId" label="productSnapshotId" />-->
        <el-table-column v-if="columns.visible('brandId')" prop="brandId" label="活动" />
<!--        <el-table-column v-if="columns.visible('categoryId')" prop="categoryId" label="分类id" />-->
        <el-table-column v-if="columns.visible('nftCategoryName')" prop="nftCategoryName" label="nft分类名称" />
        <el-table-column v-if="columns.visible('number')" prop="number" label="总数量" />
        <el-table-column v-if="columns.visible('nftCode')" prop="nftCode" label="nft编码" />
        <el-table-column v-if="columns.visible('name')" prop="name" label="nft名称" />
          <el-table-column v-if="columns.visible('pic')" prop="pic" label="主图">
            <template slot-scope="scope">
              <a :href="scope.row.pic" style="color: #42b983" target="_blank">
                <img :src="scope.row.pic" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
          <el-table-column v-if="columns.visible('albumPics')" prop="albumPics" label="画册图片">
            <template slot-scope="scope">
              <a :href="scope.row.albumPics" style="color: #42b983" target="_blank">
                <img :src="scope.row.albumPics" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
        <el-table-column v-if="columns.visible('publishStatus')" prop="publishStatus" label="上架状态">
          <template slot-scope="scope">
            {{ dict.label.nft_status[scope.row.publishStatus] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('sort')" prop="sort" label="排序" />
        <el-table-column v-if="columns.visible('price')" prop="price" label="金额" />
        <el-table-column v-if="columns.visible('weight')" prop="weight" label="商品重量，默认为克" />
        <el-table-column v-if="columns.visible('detailHtml')" prop="detailHtml" label="产品详情网页内容" />
        <el-table-column v-if="columns.visible('detailMobileHtml')" prop="detailMobileHtml" label="详情" />
        <el-table-column v-if="columns.visible('brandName')" prop="brandName" label="品牌名称" />
        <el-table-column v-if="columns.visible('productAttr')" prop="productAttr" label="nft属性" />
        <el-table-column v-if="columns.visible('createBy')" prop="createBy" label="创建人" />
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
<!--        <el-table-column v-if="columns.visible('updateBy')" prop="updateBy" label="修改人" />-->
<!--        <el-table-column v-if="columns.visible('updateTime')" prop="updateTime" label="修改时间">-->
<!--          <template slot-scope="scope">-->
<!--            <span>{{ parseTime(scope.row.updateTime) }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column v-permission="['admin','nft:edit','nft:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudNft from '@/api/nft'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";

// crud交由presenter持有
const defaultCrud = CRUD({ title: 'nft', url: 'api/nft', sort: 'nftId,desc', crudMethod: { ...crudNft }})
const defaultForm = { nftId: null, productSnapshotId: null, brandId: null, categoryId: null,number: null, nftCode: null, name: null, pic: null, albumPics: null, publishStatus: null, sort: null, price: null, weight: null, detailHtml: null, detailMobileHtml: null, brandName: null, nftCategoryName: null, createBy: null, createTime: null, updateBy: null, updateTime: null, productAttr: null }
export default {
  name: 'Nft',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['nft_status'],
  data() {
    return {
      picArr:[],albumPicsArr:[],
      permission: {
        add: ['admin', 'nft:add'],
        edit: ['admin', 'nft:edit'],
        del: ['admin', 'nft:del']
      },
      rules: {
        name: [
          { required: true, message: 'nft名称不能为空', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '总数量不能为空', trigger: 'blur' }
        ]
      }    }
  },
  watch: {
    "picArr":function(val) {
      if (val) {
        this.form.pic = val.join(",");
      }
    },
    "albumPicsArr":function(val) {
      if (val) {
        this.form.albumPics = val.join(",");
      }
    },
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.pic && form.id) {
        this.picArr = form.pic.split(",");
      }else{
        this.picArr=[]
      }
      if (form.albumPics && form.id) {
        this.albumPicsArr = form.albumPics.split(",");
      }else{
        this.albumPicsArr=[]
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
